package com.partner.action.order;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.partner.bean.sysright.TSysrole;
import com.partner.bean.sysright.TSysuserVo;
import com.partner.frame.common.GetProPertiesUtil;
import com.partner.service.sysright.SysRoleManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.partner.bean.member.Member;
import com.partner.bean.order.Ordermi;
import com.partner.bean.sysright.TSysuser;
import com.partner.frame.Result;
import com.partner.frame.common.CommonInstans;
import com.partner.frame.common.ConstantsMark;
import com.partner.frame.util.MemberUtil;
import com.partner.service.member.MemberService;
import com.partner.service.order.OrderService;
import com.partner.service.order.OrdermiService;
import com.partner.service.sysright.SysManagerService;

@Controller
@RequestMapping("/ordermi")
public class OrdermiAction {

	@Autowired
	private MemberService memberService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrdermiService ordermiService;
	@Autowired
	private SysManagerService sysManagerService;
	@Autowired
	private SysRoleManagerService sysRoleManagerService;
	
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request,Ordermi ordermi
			) throws ParseException{
		ModelAndView result = new ModelAndView();

		List<Ordermi> ordermiList = ordermiService.selectAll(ordermi);
		
		result.addObject("ordermi", ordermi);
		result.addObject("ordermiList", ordermiList);
		result.setViewName("/pages/ordermi/list");
		return result;
	}
	
	
	@RequestMapping(value = "/mySalelist")
	public ModelAndView mySalelist(HttpServletRequest request,Ordermi ordermi) throws ParseException{
		ModelAndView result = new ModelAndView();
		
		String sysusername = (String)request.getSession().getAttribute(CommonInstans.SYSUSERNAME);
		int sysuserid = (int)request.getSession().getAttribute(CommonInstans.LOGINUSERID);
		TSysuser user = sysManagerService.selectByPrimaryKey(sysuserid);
		ordermi.setType(2);
		if(!sysusername.equals("admin")){
			ordermi.setSysuserid(sysuserid);
		}

		Integer adminRoleId = getAdminRoleId();
		if(adminRoleId != null){
			TSysuser tSysuser = new TSysuser();
			tSysuser.setUsername(sysusername);
			//根据登录名查询出登录用户
			TSysuserVo sysUser = sysManagerService.getSysUserbyUsername(tSysuser);
			if(sysUser != null ){
				if(adminRoleId.equals(sysUser.getRoleid())){
					ordermi.setSysuserid(null);
				}
			}
		}
		List<Ordermi> ordermiList = ordermiService.selectAll(ordermi);
		
		result.addObject("ordermi", ordermi);
		result.addObject("ordermiList", ordermiList);
		result.setViewName("/pages/ordermi/mysalelist");
		return result;
	}
	private Integer getAdminRoleId() {
		//查找是否有管理员角色
		String adminRoleName = "管理员";
		try {
			adminRoleName = GetProPertiesUtil.getProperties("system.properties","admin.rolename");
		} catch (Exception e) {
		}
		List<TSysrole> roles = sysRoleManagerService.getAllRoles();
		for(TSysrole role : roles){
			if(adminRoleName.equals(role.getRolename())){
				return  role.getId();
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/activation",method = RequestMethod.POST)
	@ResponseBody
	public Result activation(HttpServletRequest request,HttpServletResponse response,Integer id){
		Result result = new Result();
		Ordermi ordermi = ordermiService.selectByPrimaryId(id);
		if(ordermi.getStatus()==0){  //只处理未激活的数据
			if(ordermi.getType()==1){
				MemberUtil.changeAccountAmount(id, memberService, ordermiService, orderService);//账户激活
				MemberUtil.addAccount(ordermiService, sysManagerService, id); //添加账号
				MemberUtil.setMembernum(ordermi.getMemberid(),memberService);//变更团队人数和等级
			}else if(ordermi.getType()==2){
				MemberUtil.changeSaleAmount(id,memberService,orderService,ordermiService);//购买激活
			}
			result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		}else{
			result.setDesc("请勿重复激活");
		}
		return result;
	}
	
	@RequestMapping(value = "/activations",method = RequestMethod.POST)
	@ResponseBody
	public Result activations(HttpServletRequest request,HttpServletResponse response,String param){
		Result result = new Result();
		String[] ids = param.split(",");
		for(int i=0;i<ids.length;i++){
			Ordermi ordermi = ordermiService.selectByPrimaryId(Integer.parseInt(ids[i]));
			if(ordermi.getStatus()==0){  //只处理未激活的数据
				if(ordermi.getType()==1){
					MemberUtil.changeAccountAmount(Integer.parseInt(ids[i]), memberService, ordermiService, orderService);//账户激活
					MemberUtil.addAccount(ordermiService, sysManagerService, Integer.parseInt(ids[i]));
					MemberUtil.setMembernum(ordermi.getMemberid(),memberService);//变更团队人数和等级
				}else if(ordermi.getType()==2){
					MemberUtil.changeSaleAmount(Integer.parseInt(ids[i]),memberService,orderService,ordermiService);//购买激活
				}
			}
			
		}
		result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/report4")
	public ModelAndView report3(HttpServletRequest request,Ordermi ordermi) throws ParseException{
		ModelAndView result = new ModelAndView();
		List<Ordermi> ordermi1 = ordermiService.report4(ordermi);//拥有开户权限的管理员的开户人数
		for(int i=0; i<ordermi1.size(); i++){
			Integer sysuserid = ordermi1.get(i).getSysuserid();
			Integer totalamount = ordermiService.getTotalAmountByUserId(sysuserid);
			ordermi1.get(i).setSettleaccounts(totalamount);
		}
		result.addObject("ordermi1", ordermi1);
		result.addObject("ordermi", ordermi);
		result.setViewName("/pages/report/report4");
		return result;
	}
	
	
	@RequestMapping(value = "/saledetail")
	public ModelAndView saledetail(HttpServletRequest request,Ordermi ordermi) throws ParseException{
		ModelAndView result = new ModelAndView();
		ordermi.setStatus(1);
		List<Ordermi> ordermi1 = ordermiService.selectAll(ordermi);//拥有开户权限的管理员的开户人数
		
		result.addObject("ordermi1", ordermi1);
		result.addObject("ordermi", ordermi);
		result.setViewName("/pages/ordermi/saledetail");
		return result;
	}
	
	
	@RequestMapping(value = "/cancel",method = RequestMethod.POST)
	@ResponseBody
	public Result cancel(HttpServletRequest request,HttpServletResponse response,Integer id){
		Result result = new Result();
		Ordermi ordermi = ordermiService.selectByPrimaryId(id);
		ordermi.setStatus(2);
		if(ordermi.getMemberid()!=null){
			Member member = memberService.selectByPrimaryId(ordermi.getMemberid());
			member.setDisabled(2);
			memberService.updateByPrimaryKey(member);
		}
		ordermiService.updateByPrimaryKey(ordermi);
		result.setDesc(ConstantsMark.INFO_UPDATESUCCESS);
		return result;
	}
}
